/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.server.impl;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import com.bullionxpress.server.application.ServerGlobals;
import com.bullionxpress.shared.model.Instrument;
import com.bullionxpress.shared.util.Log;
import com.bullionxpress.shared.model.Tick;
import com.bullionxpress.shared.listeners.QuoteListener;
import com.bullionxpress.shared.model.Quote;
import com.bullionxpress.shared.util.Utility;

import com.bullionxpress.server.impl.TickManager;
import com.bullionxpress.server.impl.QuoteManager;
import com.bullionxpress.shared.service.ChartService;
import com.bullionxpress.shared.service.NoPriceAvailableException;
import com.bullionxpress.shared.service.QuoteService;
import com.bullionxpress.shared.util.SymbolList;

class TickTask extends TimerTask {

    //Vector tickList = new Vector();
    Vector listeners = new Vector();
    //ChartView chart = null;

    public TickTask(Vector listeners) {
        //this.tickList = target;
        this.listeners = listeners;
        //this.chart = c;
    }

    @Override
    public void run() {
        try {
            String[] instruments = SymbolList.list;
            for(int ctr = 0; ctr < instruments.length; ctr++ ){
                double o, h, l, c;

                QuoteService qm = new QuoteManager();

                Instrument instr = new Instrument(instruments[ctr]);
                Quote qt = qm.getQuote(instr);
                o = qt.getAsk();
                h = qt.getAsk();
                l = qt.getBid();
                c = qt.getBid();


                Tick tick = new Tick();
                tick.setInstrument(instr);
                tick.setId(Utility.generateId());
                tick.setOpen(o);
                tick.setHigh(h);
                tick.setLow(l);
                tick.setClose(c);
                tick.setVolume(0);
                tick.setTimestamp(System.currentTimeMillis());
                //tickList.add(tick);

                Iterator i = listeners.iterator();
                //Quote q = qm.getQuote(tick.getInstrument());
                //chart.add(tick);
                Log.info("Quote:" + qt.toString());
                while (i.hasNext()) {
                    QuoteListener cl = (QuoteListener) i.next();
                    cl.quote(qt);
                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

/**
 *
 * @author BullionXpress.com
 */
public class ChartProducer extends java.rmi.server.UnicastRemoteObject implements ChartService {

    Timer timer = new Timer();
    //Vector tickList = new Vector();
    Vector listeners = new Vector();

    public ChartProducer() throws RemoteException {
        
        TimerTask task = new TickTask(listeners);
        timer.scheduleAtFixedRate(task, 0, ServerGlobals.updateTime);
    }

    public void addChartListener(QuoteListener ql) {
        listeners.add(ql);
    }
    

    public Tick[] getHistory(Instrument instrument) {
        return getHistory(instrument, null);
    }

    public Tick[] getHistory(Instrument instrument, Date before) {
        //throw new UnsupportedOperationException("Not supported yet.");
        try {
            TickManager qd = new TickManager();
            //qd.connect();
            Tick[] tl = null;
            //if( before == null ){
            //    tl = qd.getAll();
            //} else {
                tl = qd.getAllAfterDateByInstrument(instrument, before);
            //}
            Log.info("Adding:" + tl.length + " ticks");
            Vector v = new Vector();
            
            for (int i = 0; i < tl.length; i++) {
                v.add(tl[i]);
            }

            Iterator i = v.iterator();
            int sz = v.size();
            Tick[] list = new Tick[sz];
            int index = 0;
            while (i.hasNext()) {
                Tick t = (Tick) i.next();
                list[index++] = t;
            }
            return list;
        } catch(Exception ex){
            return null;
        }
    }
}
